package keqiang.com.kq_flutter_core_widget;

import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.EventChannel;
import keqiang.com.kq_flutter_core_widget.channel.NativeBleChannel;
import keqiang.com.kq_flutter_core_widget.channel.NativeBleConnectStateHandler;
import keqiang.com.kq_flutter_core_widget.channel.NativeBleEnableHandler;
import keqiang.com.kq_flutter_core_widget.channel.NativeBleNotifyHandler;
import keqiang.com.kq_flutter_core_widget.channel.NativeBleScanHandler;
import keqiang.com.kq_flutter_core_widget.channel.NativeFilePickerChannel;
import keqiang.com.kq_flutter_core_widget.channel.NativeKeyValueChannel;
import keqiang.com.kq_flutter_core_widget.channel.NativeLocationChannel;
import keqiang.com.kq_flutter_core_widget.channel.NativeNavigateChannel;
import keqiang.com.kq_flutter_core_widget.channel.NativeNoticeChannel;
import keqiang.com.kq_flutter_core_widget.channel.NativeOSSChannel;
import keqiang.com.kq_flutter_core_widget.channel.NativeOSSProgressHandler;
import keqiang.com.kq_flutter_core_widget.channel.NativePushChannel;
import keqiang.com.kq_flutter_core_widget.channel.NativeThirdLoginChannel;

/**
 * KqFlutterCoreWidgetPlugin
 */
public class KqFlutterCoreWidgetPlugin implements FlutterPlugin, ActivityAware {
    
    private EventChannel mOSSEventChannel;
    private NativeOSSChannel mOSSChannel;
    private NativeNavigateChannel mNavigateChannel;
    private NativePushChannel mNativePushChannel;
    private NativeThirdLoginChannel mThirdLoginChannel;
    private NativeKeyValueChannel mKeyValueChannel;
    private NativeLocationChannel mLocationChannel;
    private NativeFilePickerChannel mFilePickerChannel;
    
    private NativeNoticeChannel mNoticeChannel;
    private NativeBleChannel mNativeBleChannel;
    
    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        mOSSEventChannel = new EventChannel(flutterPluginBinding.getBinaryMessenger(), "flutter.keqiang.com/ossEvent");
        NativeOSSProgressHandler nativeOSSProgressHandler = new NativeOSSProgressHandler();
        mOSSEventChannel.setStreamHandler(nativeOSSProgressHandler);
        mOSSChannel = new NativeOSSChannel(flutterPluginBinding.getBinaryMessenger());
        mOSSChannel.setProgressHandler(nativeOSSProgressHandler);
        
        EventChannel bleScanEventChannel = new EventChannel(flutterPluginBinding.getBinaryMessenger(), "flutter.keqiang.com/bleScanEvent");
        EventChannel bleNotifyEventChannel = new EventChannel(flutterPluginBinding.getBinaryMessenger(), "flutter.keqiang.com/bleNotifyEvent");
        EventChannel bleEnableEventChannel = new EventChannel(flutterPluginBinding.getBinaryMessenger(), "flutter.keqiang.com/bleEnableEvent");
        EventChannel bleConnectStateEventChannel = new EventChannel(flutterPluginBinding.getBinaryMessenger(), "flutter.keqiang.com/bleConnectStateEvent");
        NativeBleScanHandler bleScanHandler = new NativeBleScanHandler();
        NativeBleNotifyHandler bleNotifyHandler = new NativeBleNotifyHandler();
        NativeBleEnableHandler bleEnableHandler = new NativeBleEnableHandler();
        NativeBleConnectStateHandler bleConnectStateHandler = new NativeBleConnectStateHandler();
        bleScanEventChannel.setStreamHandler(bleScanHandler);
        bleNotifyEventChannel.setStreamHandler(bleNotifyHandler);
        bleEnableEventChannel.setStreamHandler(bleEnableHandler);
        bleConnectStateEventChannel.setStreamHandler(bleConnectStateHandler);
        
        mNativeBleChannel = new NativeBleChannel(flutterPluginBinding.getBinaryMessenger());
        mNativeBleChannel.setScanHandler(bleScanHandler);
        mNativeBleChannel.setNotifyHandler(bleNotifyHandler);
        mNativeBleChannel.setBleEnableHandler(bleEnableHandler);
        mNativeBleChannel.setBleConnectStateHandler(bleConnectStateHandler);
        
        mNavigateChannel = new NativeNavigateChannel(flutterPluginBinding.getBinaryMessenger());
        mNativePushChannel = new NativePushChannel(flutterPluginBinding.getBinaryMessenger());
        mThirdLoginChannel = new NativeThirdLoginChannel(flutterPluginBinding.getBinaryMessenger());
        mKeyValueChannel = new NativeKeyValueChannel(flutterPluginBinding.getBinaryMessenger());
        mLocationChannel = new NativeLocationChannel(flutterPluginBinding.getBinaryMessenger());
        mFilePickerChannel = new NativeFilePickerChannel(flutterPluginBinding.getBinaryMessenger());
        mNoticeChannel = new NativeNoticeChannel(flutterPluginBinding.getBinaryMessenger());
    }
    
    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        mOSSEventChannel.setStreamHandler(null);
        mOSSChannel.setMethodCallHandler(null);
        mNativeBleChannel.setMethodCallHandler(null);
        mNavigateChannel.setMethodCallHandler(null);
        mNativePushChannel.setMethodCallHandler(null);
        mThirdLoginChannel.setMethodCallHandler(null);
        mKeyValueChannel.setMethodCallHandler(null);
        mLocationChannel.setMethodCallHandler(null);
        mFilePickerChannel.setMethodCallHandler(null);
        mNoticeChannel.setMethodCallHandler(null);
    }
    
    @Override
    public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
        if (mOSSChannel != null) {
            mOSSChannel.setContext(binding.getActivity());
        }
        
        if (mNativeBleChannel != null) {
            mNativeBleChannel.setContext(binding.getActivity());
        }
        
        if (mFilePickerChannel != null) {
            mFilePickerChannel.onAttachedToActivity(binding);
        }
        
        if (mNavigateChannel != null) {
            mNavigateChannel.setContext(binding.getActivity());
        }
        
        if (mNativePushChannel != null) {
            mNativePushChannel.setContext(binding.getActivity());
        }
        
        if (mKeyValueChannel != null) {
            mKeyValueChannel.setContext(binding.getActivity());
        }
        
        if (mThirdLoginChannel != null) {
            mThirdLoginChannel.onAttachedToActivity(binding);
        }
        
        if (mNoticeChannel != null) {
            mNoticeChannel.setContext(binding.getActivity());
        }
        
        if (mLocationChannel != null) {
            mLocationChannel.onAttachedToActivity(binding);
        }
    }
    
    @Override
    public void onDetachedFromActivityForConfigChanges() {
    }
    
    @Override
    public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
    }
    
    @Override
    public void onDetachedFromActivity() {
        if (mThirdLoginChannel != null) {
            mThirdLoginChannel.onDetachedFromActivity();
        }
    }
}
